var DEBUGGER = false;
var AUTHORIZER_APPID = 'wx36bea992a3856d4a'; // mofa
// var AUTHORIZER_APPID = 'wx022ec8687b780a34'; // panx

var SHAKE_THRESHOLD = 2000; // 速度

var COUPON = '/api/may-promo/promo-coupon';


if (DEBUGGER) {
    var vConsole = new VConsole();
}


var shake = new Vue({
    el: '#root',
    mixins: [mayPromoMixin],
    data: function () {
        return {
            last_x:0,
            last_y:0,
            last_z:0,
            last_update:0, // 上一次时间
            coupons:[],
            is_play: false,
            is_visible_modal: false,
            is_visible_coupons: false, // 是否显示卡券?
            user_info:null,
        }
    },

    methods: {

        /**
         *  获取 coupon
         */
        createCoupon: function (params) {
            

            // 是否有卡券?
            if(this.hasCoupon()) {
                this.showCouponsModal();
                weui.alert('只能参与一次抽奖');
                return;
            }
            // 正在请求
            if (this.is_play) {
                return;
            }
            this.is_play = true;
            var self = this;
            axios.post(COUPON,params)
                .then(function (response) {
                    if (response.data.code !== 0) {
                        throw new Error(response.data.error);
                    } else {
                        console.log(response.data);
                        self.coupons = response.data.coupon_lists;
                        self.showCouponsModal();
                    }
                })
                .catch(function (error) {
                    alert(error.message);
                    console.log(error);
                })
                .then(function () {
                    self.is_play = false;
                })
        },

        /**
         *  判断是否已经订阅
         */
        hasSubscribe: function(){
            return  this.user_info &&　parseInt(this.user_info.subscribe,10) === 1;
        },

        /**
         *  是否有卡券
         * @returns {boolean}
         */
        hasCoupon: function() {
            return !!this.coupons.length;
        },

        showCouponsModal: function () {
            this.is_visible_coupons = true;
        },

        /**
         *  关闭卡券列表
         */
        closeCouponsModal: function () {
            this.is_visible_coupons = false;
        },

        /**
         *  监听设备运动
         */
        listenDeviceMotion: function () {
            window.addEventListener('devicemotion', this.handleDeviceMotion, false);
        },


        /**
         * 处理设备运动
         */
        handleDeviceMotion: function (eventData) {
            var self = this;
            var acceleration = eventData.accelerationIncludingGravity;
            var curTime = new Date().getTime();
            if ((curTime - this.last_update) > 100) {
                var diffTime = curTime - this.last_update;
                this.last_update = curTime;
                x = acceleration.x;
                y = acceleration.y;
                z = acceleration.z;
                var speed = Math.abs(x + y + z - this.last_x - this.last_y - this.last_z) / diffTime * 10000;
                if (speed > SHAKE_THRESHOLD) {
                    // doResult();
                    // console.log(speed);
                    self.createCoupon({
                        appid: AUTHORIZER_APPID,
                        openid: this.openid
                    })

                }
                this.last_x = x;
                this.last_y = y;
                this.last_z = z;
            }
        },

        /**
         *  摇一摇
         */
        shake: function () {
            this.createCoupon({
                appid: AUTHORIZER_APPID,
                openid: this.openid
            })
        },

        /**
         *  关闭modal
         */
        closeQrcodeModal: function () {
            this.is_visible_modal =false;
        },

        showQrcodeModal: function () {
            this.is_visible_modal = true;
        },

        /**
         *  颜色类名
         */
        getClassName(discount) {
            var value = parseInt(discount);
            var classObject = {
                purple: false,
                yellow:false,
                blue:false,
                default: false,
            };
            switch (value) {
                case 500 :
                    classObject.purple = true;
                    break;
                case 300 :
                    classObject.yellow = true;
                    break;
                case 200 :
                    classObject.blue = true;
                    break;
                default:
                    classObject.blue = true;
                    break;
            }
            return classObject;
        },

        /**
         * 请求卡券列表
         * @param openid
         */
        handleFetchCouponIndex(openid){
            var self = this;
            this.fetchCouponIndex({
                appid:AUTHORIZER_APPID ,
                openid:openid
            }).then(function (coupons) {
                if (coupons instanceof Array) {
                    self.coupons = coupons;
                }
            })
        },

        checkCoupons: function() {
          // 是否订阅
          this.closeCouponsModal();
          this.showQrcodeModal();
          return;
          
          window.open(location.origin +'/may-promo/coupon/index.html?v'+Math.random());
        },

        /**
         *  超时重载
         */
        reloadPage: function () {
            window.location.reload();
        }


    },

    mounted: function () {
        // var timer = window.setTimeout(this.reloadPage, 3000);
        var self = this;
        var loading = weui.loading('加载中...');
        this.resolveOpenId()
            .then(function (openid) {
                self.openid = openid;
                // window.clearTimeout(timer); // 清除
                // 请求coupon列表
                self.handleFetchCouponIndex(openid);

                // 请求用户信息
                self.fetchUserInfo(openid)
                    .then(function (user_info) {
                        if (user_info) {
                            self.user_info = user_info;
                        }
                    })
            })
            .then(function () {
                loading.hide();
            });
        this.listenDeviceMotion();

    },

});
